<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品编码" prop="goodNum">
      <el-input v-model="dataForm.goodNum" placeholder="商品编码"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="goodName">
      <el-input v-model="dataForm.goodName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品类型" prop="goodTypeId">
      <el-input v-model="dataForm.goodTypeId" placeholder="商品类型"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="goodImg">
      <el-input v-model="dataForm.goodImg" placeholder="商品图片"></el-input>
    </el-form-item>
    <el-form-item label="商品单价" prop="goodPrice">
      <el-input v-model="dataForm.goodPrice" placeholder="商品单价"></el-input>
    </el-form-item>
    <el-form-item label="是否上架(1上架 2下架)" prop="goodUpStatus">
      <el-input v-model="dataForm.goodUpStatus" placeholder="是否上架(1上架 2下架)"></el-input>
    </el-form-item>
    <el-form-item label="商品描述" prop="goodDesc">
      <el-input v-model="dataForm.goodDesc" placeholder="商品描述"></el-input>
    </el-form-item>
    <el-form-item label="售后描述" prop="goodAfterSale">
      <el-input v-model="dataForm.goodAfterSale" placeholder="售后描述"></el-input>
    </el-form-item>
    <el-form-item label="" prop="a">
      <el-input v-model="dataForm.a" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="b">
      <el-input v-model="dataForm.b" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="c">
      <el-input v-model="dataForm.c" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
          baseURL: 'http://localhost:7001',
        visible: false,
        dataForm: {
          id: 0,
          goodNum: '',
          goodName: '',
          goodTypeId: '',
          sort: '',
          goodImg: '',
          goodPrice: '',
          goodUpStatus: '',
          goodDesc: '',
          goodAfterSale: '',
          a: '',
          b: '',
          c: ''
        },
        dataRule: {
          goodNum: [
            { required: true, message: '商品编码不能为空', trigger: 'blur' }
          ],
          goodName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          goodTypeId: [
            { required: true, message: '商品类型不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          goodImg: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          goodPrice: [
            { required: true, message: '商品单价不能为空', trigger: 'blur' }
          ],
          goodUpStatus: [
            { required: true, message: '是否上架(1上架 2下架)不能为空', trigger: 'blur' }
          ],
          goodDesc: [
            { required: true, message: '商品描述不能为空', trigger: 'blur' }
          ],
          goodAfterSale: [
            { required: true, message: '售后描述不能为空', trigger: 'blur' }
          ],
          a: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          b: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          c: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
              axios({
              url: `${this.baseURL}/hospital_goods/goodtable/info/${this.dataForm.id}`,
              method: 'get',
              params: '',
              // `responseType` 表示浏览器将要响应的数据类型
              // 选项包括: 'arraybuffer', 'document', 'json', 'text', 'stream'
              responseType: 'json'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodNum = data.goodTable.goodNum
                this.dataForm.goodName = data.goodTable.goodName
                this.dataForm.goodTypeId = data.goodTable.goodTypeId
                this.dataForm.sort = data.goodTable.sort
                this.dataForm.goodImg = data.goodTable.goodImg
                this.dataForm.goodPrice = data.goodTable.goodPrice
                this.dataForm.goodUpStatus = data.goodTable.goodUpStatus
                this.dataForm.goodDesc = data.goodTable.goodDesc
                this.dataForm.goodAfterSale = data.goodTable.goodAfterSale
                this.dataForm.a = data.goodTable.a
                this.dataForm.b = data.goodTable.b
                this.dataForm.c = data.goodTable.c
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            axios({
              url: `${this.baseURL}/hospital_goods/goodtable/${!this.dataForm.id ? 'save' : 'update'}`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'goodNum': this.dataForm.goodNum,
                'goodName': this.dataForm.goodName,
                'goodTypeId': this.dataForm.goodTypeId,
                'sort': this.dataForm.sort,
                'goodImg': this.dataForm.goodImg,
                'goodPrice': this.dataForm.goodPrice,
                'goodUpStatus': this.dataForm.goodUpStatus,
                'goodDesc': this.dataForm.goodDesc,
                'goodAfterSale': this.dataForm.goodAfterSale,
                'a': this.dataForm.a,
                'b': this.dataForm.b,
                'c': this.dataForm.c
              },
            // `responseType` 表示浏览器将要响应的数据类型
            // 选项包括: 'arraybuffer', 'document', 'json', 'text', 'stream'
            responseType: 'json'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
